package com.gaofei.controller;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.gaofei.domain.Category;
import com.gaofei.domain.HgUser;
import com.gaofei.domain.Spu;
import com.gaofei.domain.SpuVo;
import com.gaofei.service.CatService;
import com.gaofei.service.SpuService;
import com.github.pagehelper.PageInfo;

@Controller
public class IndexController {

	@Reference
	CatService catService;
	
	
	@Reference
	SpuService spuService;
	
	@Autowired
	RedisTemplate redisTemplate;
	
	@RequestMapping(value= {"","/","index"})
	public String index(Model m,HttpServletRequest req) {
		List<Category> cats = catService.getTree(0);
		m.addAttribute("cats", cats);
		
		
		
//		回显名称,从redis中取出对应的用户信息用来回显
		//先从cookie中获取用户的唯一标示
		/*Cookie[] cookies = req.getCookies();
		for (Cookie cookie : cookies) {
			if(cookie!=null&&cookie.getName().equals("hg_user_cookie")) {
				String uid = cookie.getValue();
				System.err.println(uid+"============");
				//根据唯一标示从redis中获取该用户
				HgUser redisUser = (HgUser) redisTemplate.opsForValue().get(uid);
				System.out.println(redisUser+"----------------");
				m.addAttribute("user", redisUser);
			}
		}*/
		return "index";
	}
	
	@RequestMapping("findSpuByCatId")
	public String findSpuByCatId(Integer catId,Model m,@RequestParam(defaultValue="1")int pageNum,@RequestParam(defaultValue="30")int pageSize) {
		SpuVo vo = new SpuVo();
		vo.setCategoryId(catId);
		PageInfo<Spu> info = spuService.list(pageNum, pageSize, vo);
		m.addAttribute("info", info);
		return "list";
	}
}
